package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.foamtrace.photopicker.intent.PhotoPreviewIntent;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.ImageLoaderUtils;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.LogisticsInfo;
import com.yy.jhl.ui.main.activity.PhotosShowActivity;
import com.yy.jhl.ui.manager.adapter.PictureAdapter;
import com.yy.jhl.video.MoviePlayActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 出库准备 显示界面
 * Created by libingn on 2017/12/11.
 */

public class OutboundViewActivity extends BaseActivity {
    @BindView(R.id.order_name_text)
    TextView orderNameText;
    @BindView(R.id.order_code_text)
    TextView orderCodeText;
    @BindView(R.id.auth_letter_img)
    ImageView authLetterImg;
    @BindView(R.id.note_text)
    TextView noteText;
    @BindView(R.id.time_text)
    TextView timeText;
    LogisticsInfo logisticsInfo;
    @BindView(R.id.check_hint_text)
    TextView checkHintText;
    @BindView(R.id.letter_parent)
    LinearLayout letterParent;
    @BindView(R.id.product_text)
    TextView productText;
    @BindView(R.id.order_product_more_text)
    TextView productMoreText;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    String status = "";//审核状态1、默认2、通过3、拒绝4、留言
    //1 :供应商 2：采购商
    private int currentLoginType = 1;

    @BindView(R.id.noScrollgridview)
    GridView noScrollgridview;
    PictureAdapter pictureAdapter;
    ArrayList<String> attachPaths = new ArrayList<>();
    public static void startAction(Context mContext, LogisticsInfo logisticsInfo, String status) {
        Intent intent = new Intent(mContext, OutboundViewActivity.class);
        intent.putExtra("status", status);
        intent.putExtra("logisticsInfo", logisticsInfo);
        mContext.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_outbound_view;
    }

    @Override
    public void initView() {
        currentLoginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        status = getIntent().getStringExtra("status");
        logisticsInfo = (LogisticsInfo) getIntent().getSerializableExtra("logisticsInfo");
        showTitle("出库准备", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        //审核状态1、默认2、通过3、拒绝4、留言
        switch (status) {
            case "1":
                checkHintText.setVisibility(View.VISIBLE);
                break;
            case "2":
                checkHintText.setVisibility(View.VISIBLE);
                checkHintText.setText("审核通过！    " + logisticsInfo.getUpdate_time());
                checkHintText.setTextColor(getResources().getColor(R.color.green_color));
                break;
            case "3":
                checkHintText.setVisibility(View.VISIBLE);
                checkHintText.setText("审核不通过！    " + logisticsInfo.getUpdate_time());
                checkHintText.setTextColor(getResources().getColor(R.color.red_text_color));
                if (currentLoginType == 1){
                    ensureBtn.setVisibility(View.VISIBLE);
                }
                break;
            case "4":
                checkHintText.setVisibility(View.VISIBLE);
                checkHintText.setText("审核有留言！    " + logisticsInfo.getRemark() + logisticsInfo.getUpdate_time());
                break;
        }
        orderNameText.setText(logisticsInfo.getTitle());
        orderCodeText.setText(logisticsInfo.getOrder_sn());
        String batchInfo = logisticsInfo.getBatch_info().getItems();
        int batchInfoTotal = logisticsInfo.getBatch_info().getTotal();
        if (batchInfoTotal > 2){
            productMoreText.setVisibility(View.VISIBLE);
        }else {
            productMoreText.setVisibility(View.GONE);
        }
        productText.setText(StringUtil.isEmpty(batchInfo) ? "" : batchInfo.replace("--","\n"));
        final String attach = logisticsInfo.getAttach();
//        if (StringUtil.isEmpty(attach)) {
//            letterParent.setVisibility(View.GONE);
//        } else {
//            letterParent.setVisibility(View.VISIBLE);
//            ImageLoaderUtils.display(mContext, authLetterImg, attach);
//            authLetterImg.setOnClickListener(new OnNoDoubleClickListener() {
//                @Override
//                protected void onNoDoubleClick(View v) {
//                    ArrayList<String> paths = new ArrayList<String>();
//                    paths.add(attach);
//                    PhotosShowActivity.startAction(mContext,paths,0);
//                }
//            });
//        }
        noteText.setText(logisticsInfo.getRemark());
        timeText.setText(StringUtil.isEmpty(logisticsInfo.getUpdate_time()) ? logisticsInfo.getCreate_time() :logisticsInfo.getUpdate_time() );

        pictureAdapter = new PictureAdapter(mContext,false);
        noScrollgridview.setAdapter(pictureAdapter);
        noScrollgridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                if (arg2 == attachPaths.size()) {
                } else if (attachPaths.get(arg2).endsWith("mp4")){
                    //小视频
                    MoviePlayActivity.startAction(mContext,attachPaths.get(arg2));
                } else {
                    //预览
                    PhotosShowActivity.startAction(mContext,attachPaths,arg2);
                }
            }
        });
        if (!StringUtil.isEmpty(attach)){
            String[] arrays = attach.split("\\#\\*\\#");
            for (int i =0;i<arrays.length;i++){
                attachPaths.add(arrays[i]);
            }
            pictureAdapter.setSelectPaths(attachPaths);
            StringUtil.setGridViewHeightBasedOnChildren(noScrollgridview,arrays.length,4);
        }
    }
    @OnClick({R.id.ensure_btn,R.id.order_product_more_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ensure_btn:
                OutboundSubmitActivity.startAction(mContext,logisticsInfo.getOrder_id(),logisticsInfo);
                finish();
                break;
            case R.id.order_product_more_text:
                ProductListViewWebViewActivity.startAction(mContext,logisticsInfo.getOrder_id(),logisticsInfo.getBatch_id());
                break;
        }
    }
}
